package jeecg.business.thread.task.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;

import jeecg.business.entity.enter.EnterEntity;
import jeecg.business.entity.keyinfo.KeyinfoEntity;
import jeecg.business.entity.keyword.KeywordEntity;
import jeecg.business.entity.log.CollLogEntity;
import jeecg.business.entity.site.SiteEntity;
import jeecg.business.thread.task.ExecTask;

import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.clean.CommonHtmlCleanUtil;
import org.jeecgframework.core.util.clean.DongfangCaiwuHtmlCleanUtil;
import org.jeecgframework.core.util.clean.PeopleHtmlCleanUtil;
import org.jeecgframework.core.util.clean.bean.HtmlParseResult;
import org.jeecgframework.core.util.http.HttpClientUtil;
/**
 * 
 * <p>
 * 标题: 公用抓取任务
 * </p>
 * <p>
 * 描述: // TODO
 * </p>
 * 
 * @author LinYingPiaoJian(Q:71426052)
 * @version // TODO
 * @since 2015-6-6 下午2:56:28
 */
public class PeopleExecTask extends ExecTask{
	
	protected int current = 0;
	
	protected String cookies;

	public PeopleExecTask(EnterEntity enter , KeywordEntity keyword, SiteEntity site) {
		super(enter , keyword, site);
	}
	
	public void setCookies(){}
	
	public String getFetchHtml() {
		String basehref = site.getZddz();
		String word;
		try {
			word = URLEncoder.encode(keyword.getKeyword(),site.getWyzfj());
			basehref = basehref.replace(KEYWORD_FLAG, word);
			return basehref;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
			return null;
		}
	}
	
	public void run(){
		CollLogEntity log = null;
		log = saveLog();
		try{
			
			System.out.println("exec people task......");
			String basehref = site.getZddz();
			setCookies();
			
			if(StringUtil.isNotEmpty(basehref)){
				
				HtmlParseResult result = PeopleHtmlCleanUtil.fetchPeople4Html(keyword.getKeyword(), "utf-8", site, null);
				
				if(result == null || result.getChildResultList() == null) return;
				
				HtmlParseResult childResult = null;
				KeyinfoEntity keyinfo = null;
				for(HtmlParseResult child : result.getChildResultList()){
					if(keyinfoService.hasHref(child.getUrl() , enter.getId())) continue;
					try{
						childResult = CommonHtmlCleanUtil.getInfo(child.getUrl(), site, keyword.getKeyword() , null);
						childResult.setUrl(child.getUrl());
						childResult.setEventDate(child.getEventDate());
						keyinfo = new KeyinfoEntity(childResult);
						keyinfo.setQyid(enter.getId());
						System.out.println(childResult.getUrl()+"----"+childResult.getTitle()+"----"+(childResult.getBodycontent()!=null?childResult.getBodycontent().length():0)+"----"+childResult.getEventDate());
						if(filter(enter, keyinfo , keyword))		//过滤
							keyinfoService.saveKeyinfo(keyinfo);
					}catch(Exception e){
//						e.printStackTrace();
					}
				}
			}
			
		}catch(Exception e){
//				e.printStackTrace();
//				log.setSfcg("2");
//				log.setCwxx(e.getMessage());
		}finally{
			log = collLogService.get(CollLogEntity.class, log.getId());
			log.setJssj(new Date());
			collLogService.updateEntitie(log);
			System.out.println("end....");
		}			
	}

}
